package com.study.manage.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class MapUtils {

    public static Map<String, Object> convert(Object c, String... attr) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (c != null) {
            Field fields[] = c.getClass().getFields();// 获得对象所有属性
            Field field = null;
            if (attr == null || attr.length == 0) {
                for (int i = 0; i < fields.length; i++) {
                    field = fields[i];
                    try {
                        map.put(field.getName(), field.get(c));
                    } catch (Exception e) {
                    }
                }
            } else {
                for (int i = 0; i < fields.length; i++) {
                    field = fields[i];
                    for (int j = 0; j < attr.length; j++) {
                        if (attr[j].equals(field.getName())) {
                            try {
                                map.put(field.getName(), field.get(c));
                            } catch (Exception e) {
                            }
                        }
                    }
                }
            }
        }
        return map;
    }


    @SuppressWarnings({"rawtypes", "unchecked"})
    public static void copyProperties(Object c, Map<String, Object> map) {
        if (c != null) {
            Field fields[] = c.getClass().getFields();// 获得对象所有属性
            Field field = null;
            for (int i = 0; i < fields.length; i++) {
                field = fields[i];
                field.setAccessible(true);
                try {
                    Object o = map.get(field.getName());
                    if (o != null) {
                        if (o.getClass() == field.getType()) {
                            field.set(c, o);
                        } else if (field.getType() == String.class) {
                            field.set(c, (String) o);
                        } else if (field.getType() == Long.class) {
                            field.set(c, Long.valueOf(o.toString()));
                        } else if (field.getType() == Integer.class) {
                            field.set(c, Integer.valueOf(o.toString()));
                        } else if (field.getType() == Float.class) {
                            field.set(c, Float.valueOf(o.toString()));
                        } else if (field.getType() == Boolean.class) {
                            field.set(c, Boolean.valueOf(o.toString()));
                        } else if (Enum.class.isAssignableFrom(field.getType())) {
                            Class<Enum> e = (Class) field.getType();
                            field.set(c, Enum.valueOf(e, o.toString()));
                        }
                    }
                } catch (Exception e) {
                }
            }
        }
    }


    public static void copyObj(Map<String, Object> map, Object obj, String... attr) {
        try {
            Field fields[] = obj.getClass().getFields();
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                boolean flag = false;
                for (int j = 0; j < attr.length; j++) {
                    if (attr[j].equals(field.getName())) {
                        flag = true;
                    }
                }
                if (flag) {
                    continue;
                }
                if (map.containsKey(field.getName())) {
                    try {
                        String oValue = map.get(field.getName()).toString();
                        if (field.getType() == Long.class) {
                            field.set(obj, Long.valueOf(oValue));
                        } else if (field.getType() == Integer.class) {
                            field.set(obj, Integer.valueOf(oValue));
                        } else if (field.getType() == Float.class) {
                            field.set(obj, Float.valueOf(oValue));
                        } else if (field.getType() == Boolean.class) {
                            field.set(obj, Boolean.valueOf(oValue));
                        } else if (field.getType() == Double.class) {
                            field.set(obj, Double.valueOf(oValue));
                        } else if (field.getType().isEnum()) {
                            Class clazz = field.getType();
                            field.set(obj, Enum.valueOf(clazz, oValue));
                        } else {
                            field.set(obj, oValue);
                        }

                    } catch (Exception e) {
                    }
                }
            }
        } catch (Exception e) {
        }
    }

}
